﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ExcelRowFilter
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private DataTable dt = new DataTable();
        private DataTable dtFilterResult = new DataTable();
        private DataTable dtSelectedResult = new DataTable();

        public MainWindow()
        {
            InitializeComponent();
        }



        private void btnReadExcel_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                progressBar1.Minimum = 0;
                progressBar1.Maximum = 100;
                progressBar1.Value = 0;
                string fileName = "";
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.Filter = "All files (*.*)|*.*|Excel2003文件|*.xls|Excel2007文件|*.xlsx";
                openFileDialog.RestoreDirectory = true;
                openFileDialog.FilterIndex = 1;
                if (openFileDialog.ShowDialog() == true)
                {
                    progressBar1.Value += 1;
                    fileName = openFileDialog.FileName;
                    progressBar1.Value += 1;
                    dt = ExcelHelper.InputFromExcel(fileName, "");
                    progressBar1.Value += 1;
                    dt.Columns[0].ColumnName = "序号";
                    dt.Columns[1].ColumnName = "章节";
                    dt.Columns[2].ColumnName = "方法名称";
                    dt.Columns[3].ColumnName = "检测内容";
                    dt.Columns[4].ColumnName = "适用范围";
                    dt.Columns[5].ColumnName = "检测精度";
                    dt.Columns[6].ColumnName = "定位精度";
                    dt.Columns[8].ColumnName = "平管";
                    dt.Columns[9].ColumnName = "立管";
                    dt.Columns[10].ColumnName = "海管输送管";
                    dt.Columns[11].ColumnName = "海管套管";
                    dt.Columns[12].ColumnName = "陆管输送管";
                    dt.Columns[13].ColumnName = "陆管套管";
                    dt.Columns[14].ColumnName = "柔性立管";
                    dt.Columns[15].ColumnName = "水中高";
                    progressBar1.Value += 1;
                    dt.Columns[16].ColumnName = "水中低";
                    dt.Columns[17].ColumnName = "空气中高";
                    dt.Columns[18].ColumnName = "空气中低";
                    dt.Columns[19].ColumnName = "适用的水深";
                    dt.Columns[20].ColumnName = "对水导电性要求";
                    dt.Columns[21].ColumnName = "是否需要船舶配合";
                    dt.Columns[22].ColumnName = "是否需要ROV配合";
                    dt.Columns[23].ColumnName = "是否需要潜水员配合";
                    dt.Columns[24].ColumnName = "检测速度范围";
                    dt.Columns[25].ColumnName = "单次最大检测里程";
                    dt.Columns[26].ColumnName = "检测作业对管道系统组成的要求";
                    dt.Columns[27].ColumnName = "检测作业对管道系统运行参数的要求";
                    dt.Columns[28].ColumnName = "检测作业对海况条件的要求";
                    progressBar1.Value += 1;
                    for (int rowCnt = 0; rowCnt < dt.Rows.Count; )
                    {
                        if (dt.Rows[rowCnt].ItemArray[0].ToString().Trim().Equals(""))
                            dt.Rows.RemoveAt(rowCnt);
                        else
                            rowCnt++;
                    }

                    progressBar1.Value += 1;


                    System.Windows.Data.CollectionViewSource linesSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("Lines")));
                    //MessageBox.Show(Binding.);
                    progressBar1.Value += 1;

                    linesSource.Source = dt;
                    dtFilterResult.Columns.Clear();
                    foreach (DataColumn column in dt.Columns)
                    {
                        progressBar1.Value += 1;
                        dtFilterResult.Columns.Add(column.ColumnName);
                        dtSelectedResult.Columns.Add(column.ColumnName);
                    }
                    progressBar1.Value = progressBar1.Maximum;
                    btnReadExcel.Content = "已读取";
                    btnReadExcel.IsEnabled = false;
                    cbbLines.Focus();
                    cbbLines_SelectionChanged(sender, null);
                }
                else
                {
                    MessageBox.Show("用户取消");
                }


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                //throw ex;
            }
        }

      /*  private void btnCheck_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                dtFilterResult.Clear();
                if (dt.Columns.Contains("检测内容"))
                {
                    string filter = "";
                    Stack<string> filters = new Stack<string>();
                    if (!txbF1.Text.Trim().Equals(""))
                    {
                        filters.Push("检测内容 like '%" + txbF1.Text + "%'");
                    }
                    if (!txbF2.Text.Trim().Equals(""))
                    {
                        filters.Push("检测内容 like '%" + txbF2.Text + "%'");
                    }
                    if (!txbF3.Text.Trim().Equals(""))
                    {
                        filters.Push("检测内容 like '%" + txbF3.Text + "%'");
                    }
                    if (filters.Count > 0)
                        filter += filters.Pop();
                    while (filters.Count > 0)
                    {
                        filter += " and " + filters.Pop();
                    }
                    System.Windows.Data.CollectionViewSource linesSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("Lines")));
                    dt.Select(filter).CopyToDataTable(dtFilterResult, LoadOption.OverwriteChanges);
                    linesSource.Source = dtFilterResult;
                }
                else
                {
                    MessageBox.Show("Excel文件导入错误！");
                }
            }
            catch
            {
            }
        }*/

        private void cbbLines_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                dtSelectedResult.Clear();
                if (cbbLines.SelectedItem != null)
                {
                    DataRowView slRow = cbbLines.SelectedItem as DataRowView;
                    dtSelectedResult.ImportRow(slRow.Row);
                }
                dtgT1.DataContext = dtSelectedResult;
                dtgT2.DataContext = dtSelectedResult;
            }
            catch
            {
            }
        }

       /* private void txbF1_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == System.Windows.Input.Key.Enter)
                {
                    e.Handled = true;
                    btnCheck_Click(sender, null);
                }
            }
            catch { }
        }*/

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            //MessageBox.Show("a");
            dtFilterResult.Clear();
            if (dt.Columns.Contains("平管"))
            {
                string filter = "";
                Stack<string> filters = new Stack<string>();
                if(rdbG.IsChecked.Value)
                {
                    filters.Push(@"柔性立管 <> '√' or 柔性立管 is null");
                }
                if(rdbR.IsChecked.Value)
                {
                    filters.Push(@"柔性立管 = '√'");
                }
                if (ckb1.IsChecked.Value)
                {
                    filters.Push(@"平管 <> ''");
                }
                if (ckb2.IsChecked.Value)
                {
                    filters.Push(@"立管 <> ''");
                }
                if (ckb4.IsChecked.Value)
                {
                    filters.Push(@"海管输送管 <> ''");
                }
                if (ckb5.IsChecked.Value)
                {
                    filters.Push(@"陆管套管 <> ''");
                }
                if (ckb6.IsChecked.Value)
                {
                    filters.Push(@"陆管输送管 <> ''");
                }
                if (ckb7.IsChecked.Value)
                {
                    filters.Push(@"海管套管 <> ''");
                }
                if (ckb8.IsChecked.Value)
                {
                    filters.Push(@"适用的水深 not like '1*'");
                }
                if (rdbWL.IsChecked.Value)
                {
                    filters.Push(@"水中低 = '√' or 水中低 = '无要求'");
                }
                if (rdbAL.IsChecked.Value)
                {
                    filters.Push(@"空气中低 = '√' or 空气中低 = '无要求'");
                }

                if (filters.Count > 0)
                    filter += filters.Pop();
                while (filters.Count > 0)
                {
                    filter += @" and (" + filters.Pop() + @")";
                }
                System.Windows.Data.CollectionViewSource linesSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("Lines")));
                dt.Select(filter).CopyToDataTable(dtFilterResult, LoadOption.OverwriteChanges);
                linesSource.Source = dtFilterResult;
            }
            else
            {
                MessageBox.Show("Excel文件导入错误！");
            }
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ckb1.IsChecked = false;
            ckb2.IsChecked = false;
            ckb4.IsChecked = false;
            ckb5.IsChecked = false;
            ckb6.IsChecked = false;
            ckb7.IsChecked = false;
            ckb8.IsChecked = false;
            rdbWH.IsChecked = false;
            rdbWL.IsChecked = false;
            rdbAH.IsChecked = false;
            rdbAL.IsChecked = false;
            rdbR.IsChecked = false;
            rdbG.IsChecked = false;
            ckb1.IsEnabled = true;
            ckb2.IsEnabled = true;
            ckb4.IsEnabled = true;
            ckb5.IsEnabled = true;
            ckb6.IsEnabled = true;
            ckb7.IsEnabled = true;
            CheckBox_Checked(sender, null);
        }

        private void MenuItemAbout_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("A product of Rojek(Tianjin.OPR)\n\nEmail:rojecknet@live.com");
        }
        private void MenuItemClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void rdbR_Checked(object sender, RoutedEventArgs e)
        {
            ckb1.IsChecked = false;
            ckb2.IsChecked = false;
            ckb4.IsChecked = false;
            ckb5.IsChecked = false;
            ckb6.IsChecked = false;
            ckb7.IsChecked = false;

            ckb1.IsEnabled = false;
            ckb2.IsEnabled = false;
            ckb4.IsEnabled = false;
            ckb5.IsEnabled = false;
            ckb6.IsEnabled = false;
            ckb7.IsEnabled = false;

            CheckBox_Checked(sender, null);
        }

        private void rdbG_Checked(object sender, RoutedEventArgs e)
        {
            ckb1.IsEnabled = true;
            ckb2.IsEnabled = true;
            ckb4.IsEnabled = true;
            ckb5.IsEnabled = true;
            ckb6.IsEnabled = true;
            ckb7.IsEnabled = true;
            CheckBox_Checked(sender, null);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            DateTime TOut = new DateTime(2015, 7, 15);
            if(DateTime.Now > TOut)
            {
                MessageBox.Show("System Error!", "Error");
                this.Close();
            }
        }
    }
}
